// literals.cpp
// Customizer
// Protea Customizer / Business Logic for managing literals
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//////////////////////////////////////////////////////////////////////////////
//
// LITERAL_DEFN Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 2
//@ CHA Add Literal
//@ COD AddLiteral
//@ NUM 26
//@ CRI LITERAL_NAME
//@ CRI LITERAL
//@ TAB LITERAL_DEFN Write
//
//@ GEN Add Literal will add a literal value for an application
//
TRANSACTION_CODE( AddLiteral )
{
	//@ GRS Start the transaction
	START_TXN( AddLiteral );
		START_DB_TXN();
			AddLiteralInternal( &txnvars, false );
		END_DB_TXN();

		LOG_TRANSACTION();
	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};



//@ CHA Change Literal
//@ COD ChangeLiteral
//@ NUM 26
//@ CRI LITERAL_NAME
//@ CRI LITERAL
//@ TAB LITERAL_DEFN Write
//
//@ GEN Change Literal will change literal value for an application
//
TRANSACTION_CODE( ChangeLiteral )
{
	//@ GRS Start the transaction
	bool require_log = false;
	START_TXN( ChangeLiteral );
		START_DB_TXN();
			require_log = ChgLiteralInternal( &txnvars, false );
		END_DB_TXN();

		if (require_log)
			LOG_TRANSACTION();

	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};




//@ CHA Delete Literal
//@ COD DeleteLiteral
//@ NUM 27
//@ CRI LITERAL_NAME
//@ TAB LITERAL_DEFN Write
//
//@ GEN Delete Literal will delete a literal definition record
//
TRANSACTION_CODE( DeleteLiteral ) 
{
	//@ GRS Start the transaction
	START_TXN( DeleteLiteral );
		START_DB_TXN();
			DelLiteralInternal( &txnvars, false );
		END_DB_TXN();

		LOG_TRANSACTION();
	CLEANUP_TXN();
	END_TXN();
	//@ GRE End the transaction
};



//
//@ CHA Review Literals
//@ COD ReviewLiterals
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB LITERAL_DEFN Read
//
//@ GEN Returns all literals in the system
//
TRANSACTION_CODE( ReviewLiterals ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCBuffer* Literal_Values = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( ReviewLiterals );
		XCSQLGen* query = txnvars.query;

		//@ Create the LITERAL_DEFN buffer
		TransBuf = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Create the LITERAL_VALUES buffer
		Literal_Values = App->CreateBuffer( FMT_LITERAL_VALUES );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		Literal_Values->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				detail = App->CreateBuffer( child, true );

				// Formulate select statement
				query->MakeSELECT( TransBuf );

				// If Appl_nbr = 0, select all records
				XCCompare comptemp;
				XCField* temp = TransBuf->GetField( FLD_APPL_NBR );
				if (temp->Compare(0) != 0)
					query->AddWHERE( TransBuf->GetField( FLD_APPL_NBR ), &comptemp );

				// Execute the query
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				while (!stmt->Eof()) {
					//@ MoveCorresponding from LITERAL_DEFN to detail buffer
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );

					//@ Move LITERAL_NUMBER from LITERAL_DEFN to LITERAL_VALUES
					Literal_Values->MoveField( TransBuf, FLD_LITERAL_NUMBER );

					//@ SUB LiteralValueRetrieve to retrieve literal value definition
					if (LiteralValueRetrieve( Literal_Values, &txnvars )) {
						//@ MoveCorresponding from LITERAL_VALUES to detail buffer
						detail->MoveCorresponding( Literal_Values, txnvars.Errors, true );
					} else
						detail->GetField( FLD_LITERAL )->Clear();

					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 4, &TransBuf, &Literal_Values, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};






//
//@ CHA Review Literal
//@ COD ReviewLiteral
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB LITERAL_DEFN Read
//
//@ GEN Returns information on a single literal
//
TRANSACTION_CODE( ReviewLiteral ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* Literal_Values = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( ReviewLiteral );
		XCSQLGen* query = txnvars.query;

		//@ Create the LITERAL_DEFN buffer
		TransBuf = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Create the LIT_RELATE buffer
		Literal_Values = App->CreateBuffer( FMT_LITERAL_VALUES );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			//@ SUB LiteralDefnVerify to verify that the literal exists
			LiteralDefnVerify( TransBuf, &txnvars, VERIFY_REVIEW, false );

			int child = FMT_LITERAL_DETAIL;
			if (child) {
				detail = App->CreateBuffer( child, true );

				//@ Move corresponding values from the LITERAL_DEFN buffer to the LITERAL_VALUES buffer
				Literal_Values->MoveCorresponding( TransBuf, NULL );

				//@ Select all literal value records with current APPL_NBR and LITERAL_NUMBER
				query->MakeSELECT( Literal_Values );
				query->AddWHERE( 2,
					Literal_Values->GetField( FLD_APPL_NBR ),
					Literal_Values->GetField( FLD_LITERAL_NUMBER ));
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				//@ For each record found,
				while (!stmt->Eof()) {
					//@ GRS
					detail->MoveCorresponding( Literal_Values, txnvars.Errors, true );
					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

					//@ Move to next record
					stmt->MoveNext();
					//@ GRE
				};
			};
		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 4, &TransBuf, &Literal_Values, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};






//
//@ CHA Literal Where Used
//@ COD LiteralWhereUsed
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB LITERAL_DEFN Read
//
//@ GEN Returns information on a single literal
//
TRANSACTION_CODE( LiteralWhereUsed ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* Lit_Relate = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( LiteralWhereUsed );
		XCSQLGen* query = txnvars.query;

		//@ Create the LITERAL_DEFN buffer
		TransBuf = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Create the LIT_RELATE buffer
		Lit_Relate = App->CreateBuffer( FMT_LIT_RELATE );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			//@ SUB LiteralDefnVerify to verify that the literal exists
			LiteralDefnVerify( TransBuf, &txnvars, VERIFY_REVIEW, false );

			//@ Return LITERAL_DEFN information
			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			int child = FMT_ADDLITGROUP;
			if (child) {
				detail = App->CreateBuffer( child, true );

				//@ Move corresponding values from the LITERAL_DEFN buffer to the LITERAL_VALUES buffer
				Lit_Relate->MoveCorresponding( TransBuf, NULL );

				//@ Select all literal value records with current APPL_NBR and LITERAL_NUMBER
				query->MakeSELECT( Lit_Relate );
				query->AddWHERE( 2,
					Lit_Relate->GetField( FLD_APPL_NBR ),
					Lit_Relate->GetField( FLD_LITERAL_NUMBER ));
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				//@ For each record found,
				while (!stmt->Eof()) {
					//@ GRS

					//@ Move corresponding data from Lit_Relate to the detail record
					detail->MoveCorresponding( Lit_Relate, txnvars.Errors, true );

					//@ SUB LookupNameFromNumber to translate number to name
					LookupNameFromNumber( &txnvars, Lit_Relate, detail, FLD_LIT_GROUP_NBR, FLD_LIT_GROUP_NAME, FMT_LIT_GROUP );

					//@ Return the data
					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

					//@ Move to next record
					stmt->MoveNext();
					//@ GRE
				};
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 4, &TransBuf, &Lit_Relate, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};
